package tester

import (
	"math/rand"
	"strconv"
	"strings"
	"time"
)

const (
	charset = "abcdefghijklmnopqrstuvwzyz0123456789"
)

var (
	charsetSize = len(charset)
)

func init() {
	rand.New(rand.NewSource(time.Now().UnixNano()))
}

func getFuncVal(expr string) string {
	args := strings.Split(expr, " ")
	switch args[0] {
	case "random_string":
		if len(args) != 3 {
			return ""
		}

		size, err := strconv.ParseUint(args[2], 10, 32)
		if err != nil {
			return ""
		}

		return randomString(args[1], uint(size))
	default:
		return ""
	}
}

func randomString(prefix string, size uint) string {
	b := make([]byte, size)

	for i := range b {
		b[i] = charset[rand.Intn(charsetSize)]
	}

	return prefix + string(b)
}
